package xanatorium.Zeiterfassung;

import java.util.ArrayList;
import java.util.List;

import xanatorium.Zeiterfassung.EditEntry.Editstates;
import xanatorium.Zeiterfassung.data.Entry;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class Zeiterfassung extends Activity
{
	private final List<Entry> entrys = new ArrayList<Entry>();
	
	private Entry getLastEntry()
	{
		if (entrys.isEmpty())
		{
			entrys.add(new Entry());
		}
		return entrys.get(entrys.size() - 1);
	}
	
	private Entry addNewEntry()
	{
		if (entrys.isEmpty())
		{
			entrys.add(new Entry());
		}
		entrys.get(entrys.size() - 1).end();
		entrys.add(new Entry());
		return entrys.get(entrys.size() - 1);
	}
	
	/**
	 * Initialization of the Activity after it is first created. Must at least call
	 * {@link android.app.Activity#setContentView(int)} to describe what is to be displayed in the screen.
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.zeiterfassung);
		
		/*
		 * Display a text message with yes/no buttons and handle each message as well as the cancel action
		 */
		Button startButton = (Button) findViewById(R.id.startEntry);
		startButton.setOnClickListener(new myListener(new EditEntry(Editstates.start, getLastEntry())));
		
		/*
		 * Display a long text message with yes/no buttons and handle each message as well as the cancel action
		 */
		Button endButton = (Button) findViewById(R.id.endEntry);
		if (!getLastEntry().isEnded())
		{
			addNewEntry();
		}
		endButton.setOnClickListener(new myListener(new EditEntry(Editstates.stop, getLastEntry())));
		Button editCurrentButton = (Button) findViewById(R.id.editEntry);
		editCurrentButton.setOnClickListener(new myListener(new EditEntry(Editstates.full, getLastEntry())));
		
		/* Display a list of items */
		Button dayButton = (Button) findViewById(R.id.editDay);
		dayButton.setOnClickListener(new myListener(new EditDayAndroidActivity()));
		
		/* Display a list of items */
		Button weekButton = (Button) findViewById(R.id.editWeek);
		weekButton.setOnClickListener(new myListener(new EditWeekAndroidActivity()));
		
		/* Display a radio button group */
		Button editAllButton = (Button) findViewById(R.id.editAll);
		editAllButton.setOnClickListener(new myListener(new EditAllAndroidActivity()));
		
		/* Display a radio button group */
		Button exportButton = (Button) findViewById(R.id.export);
		exportButton.setOnClickListener(new myListener(new ExportActivity()));
	}
	
	private class myListener implements OnClickListener
	{
		
		private final Activity target;
		
		public myListener(Activity target)
		{
			super();
			this.target = target;
		}
		
		@Override
		public void onClick(View v)
		{
			// Request the next activity transition (here starting a new
			// one).
			startActivity(new Intent(Zeiterfassung.this, target.getClass()));
			// Supply a custom animation. This one will just fade the new
			// activity on top. Note that we need to also supply an
			// animation
			// (here just doing nothing for the same amount of time) for the
			// old activity to prevent it from going away too soon.
			fade();
		}
		
		private void fade()
		{
			overridePendingTransition(R.anim.fade, R.anim.hold);
		}
	}
}